Skip to content

Use minimum necessary division precision in BigDecimal_DoDivmod#371

Merged
tompng merged 1 commit intoruby:masterfrom
tompng:divmod_internal_div_prec
Jul 17, 2025
Merged

Use minimum necessary division precision in BigDecimal_DoDivmod#371
tompng merged 1 commit intoruby:masterfrom
tompng:divmod_internal_div_prec

Conversation

@tompng
Copy link
Member

@tompng tompng commented Jul 13, 2025

Improve performance of div and divmod in some case.

BigDecimal('7'*100000).div(BigDecimal('3'*99999)) #=> 23
# processing time: 0.566657s → 0.001474s

In the above example, div needs to calculate division with 2-digits precision. But it was internally calculating with 200000-digits precision.

This pull request also fixes the cause of this bug (found while adding test)

BigDecimal('3e20').div(BigDecimal('-7e-19'))
# => -428571428571428571428571428571000000000 (bigdecimal-3.2.1)
# => -428571428571428571428571428571428571429 (expected result)

but this bug was fixed by a side effect of #377

@tompng tompng marked this pull request as draft July 14, 2025 12:37
@tompng tompng force-pushed the divmod_internal_div_prec branch from 09f314a to d72e530 Compare July 14, 2025 13:03
@tompng tompng mentioned this pull request Jul 14, 2025
@tompng tompng force-pushed the divmod_internal_div_prec branch from d72e530 to 3b70d93 Compare July 16, 2025 19:26
@tompng tompng marked this pull request as ready for review July 16, 2025 19:52
@tompng tompng added the bug label Jul 17, 2025
Integer division part of a.divmod(b) only needs (a.exponent-b.exponent+1) digits precision.
@tompng tompng force-pushed the divmod_internal_div_prec branch from 3b70d93 to 0f9e35f Compare July 17, 2025 08:21
@tompng tompng merged commit 28903c4 into ruby:master Jul 17, 2025
79 checks passed
@tompng tompng deleted the divmod_internal_div_prec branch July 17, 2025 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant